var SalaryForm = function () {
  var template = /*html*/ `
    <div class="salary-form" v-loading.fullscreen.lock="loading">
      <div>
        <el-form :model="salary" label-width="110px">
          <el-form-item label="编号" prop="personId">
            <span>{{salary.personId}}</span>
          </el-form-item>
          <el-form-item label="工号" prop="workerNo">
            <span>{{salary.workerNo}}</span>
          </el-form-item>
          <el-form-item label="职员名称" prop="personName">
            <span>{{salary.personName}}</span>
          </el-form-item>
          <el-form-item label="部门" prop="deptName">
            <span>{{salary.deptName}}</span>
          </el-form-item>
          <el-form-item label="职务" prop="dutyStr">
            <span>{{salary.dutyStr}}</span>
          </el-form-item>
          <el-form-item label="基本工资(元)" prop="monthlySalary">
            <span>{{salary.monthlySalary}}</span>
          </el-form-item>
          <el-form-item label="请假(比例)" prop="ratioLeave">
            <el-select class="form-input" v-model="salary.ratioLeave">
              <el-option v-for="level in levelList" :key="'level_'+level.rank" :value="level.rank" :label="level.ratioLeave"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="旷工(比例)" prop="ratioAbsent">
            <el-select class="form-input" v-model="salary.ratioAbsent">
              <el-option v-for="level in levelList" :key="'level_'+level.rank" :value="level.rank" :label="level.ratioAbsent"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="加班(比例)" prop="ratioOvertime">
            <el-select class="form-input" v-model="salary.ratioOvertime">
              <el-option v-for="level in levelList" :key="'level_'+level.rank" :value="level.rank" :label="level.ratioOvertime"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="社保扣款(元)" prop="socialSecurity">
            <el-input-number class="form-input" v-model="salary.socialSecurity" :min="0" :controls="false" :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="公积金扣款(元)" prop="accumulationFund">
            <el-input-number class="form-input" v-model="salary.accumulationFund" :min="0" :controls="false" :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="其他扣款(元)" prop="otherDeductions">
            <el-input-number class="form-input" v-model="salary.otherDeductions" :min="0" :controls="false" :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="其他加项(元)" prop="otherAddition">
            <el-input-number class="form-input" v-model="salary.otherAddition" :min="0" :controls="false" :precision="2"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin-top:20px;text-align:center;">
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      salaryData: {
        type: Object,
        required: true
      },
      levelList: {
        type: Array,
        default: function () {
          return [];
        }
      }
    },

    data: function () {
      var salary = _.cloneDeep(this.salaryData);
      var fields = ['monthlySalary', 'socialSecurity', 'accumulationFund', 'otherDeductions', 'otherAddition'];
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        if (salary[field] === null) {
          salary[field] = undefined;
        }
      }
      return {
        salary: salary,
        loading: false
      };
    },

    methods: {
      save: function () {
        var salary = this.salary;
        if (!this.checkValidate(salary)) {
          return;
        }

        var postData = {};
        var formatFields = ['socialSecurity', 'accumulationFund', 'otherDeductions', 'otherAddition'];
        var fields = ['detailId', 'personId', 'ratioLeave', 'ratioAbsent', 'ratioOvertime'];
        fields.pushArray(formatFields);
        SysUtil.setPostData(postData, salary, fields);

        for (var i = 0; i < formatFields.length; i++) {
          var field = formatFields[i];
          if (postData[field] === undefined) {
            delete postData[field];
          }
        }

        this.loading = true;
        var self =this;
        ajaxRequest('dingTalk/updateFinanceDetail',postData).then(function(msg){
          self.$alert('保存职员工资明细成功','信息',{
            callback:function(){
              self.$emit('save-success');
            }
          });
          self.loading = false;
        }).catch(function(){
          self.loading = false;
        });
      },

      checkValidate: function (salary) {
        var checkFields = ['ratioLeave', 'ratioAbsent', 'ratioOvertime'];
        if (!CheckVaildUtil.checkNullValue(checkFields, salary, this.getErrorMsg)) {
          return false;
        }
        return true;
      },

      getErrorMsg: function (code) {
        var errorObj = {
          ratioLeave: '请选择请假比例',
          ratioAbsent: '请选择旷工比例',
          ratioOvertime: '请选择加班比例'
        };
        return errorObj[code];
      }
    },

    template: template
  };
}();